package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.PollutionControlDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.PollutionControl;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/*
 * Created by maowenjie on 2017/4/6.
 */
@Component
public class PollutionControlManager {
    private final PollutionControlDao pollutionControlDao;
    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public PollutionControlManager(PollutionControlDao pollutionControlDao,  ReportHandleResumeDao reportHandleResumeDao) {
        this.pollutionControlDao = pollutionControlDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }

    /**
     * 新建治污中心-- 单据
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> createPollutionControl(PollutionControl pollutionControl, Integer toStatus){
        Integer fromStatus = pollutionControl.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "ZZW";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,pollutionControlDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        pollutionControl.setNumber(number);
        pollutionControl.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            pollutionControl.setStatus(toStatus);
            pollutionControlDao.create(pollutionControl);
            map.put("pollutionControlId",pollutionControl.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(pollutionControl.getId());
            reportHandleResume.setReportNumber(pollutionControl.getNumber());
            reportHandleResume.setType(DocumentsType.POLLUTION_CONTROL.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            pollutionControlDao.create(pollutionControl);
            map.put("pollutionControlId",pollutionControl.getId());
        }
        return map;
    }


    @Transactional
    public Map<String , Object> updatePollutionControl(PollutionControl pollutionControl,Integer toStatus){
        Integer fromStatus = pollutionControl.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagPollutionControl;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            pollutionControl.setStatus(toStatus);
            flagPollutionControl = pollutionControlDao.update(pollutionControl);
            map.put("flagPollutionControl",flagPollutionControl);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(pollutionControl.getId());
            reportHandleResume.setReportNumber(pollutionControl.getNumber());
            reportHandleResume.setType(DocumentsType.POLLUTION_CONTROL.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagPollutionControl = pollutionControlDao.update(pollutionControl);
            map.put("flagPollutionControl",flagPollutionControl);
        }
        return map;
    }

    @Transactional
    public Boolean update(PollutionControl pollutionControl) {
        return pollutionControlDao.update(pollutionControl);
    }
}
